package com.eatme.web.sys.controller;

import com.google.code.kaptcha.Producer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

@Controller
@RequestMapping("captcha")
public class CaptchaController {

    private static final Logger LOG = LoggerFactory.getLogger(CaptchaController.class);

    @Autowired
    private Producer producer;

    @RequestMapping( "/record")
    public void get(HttpServletRequest request,HttpServletResponse response) throws IOException {
        response.setContentType("image/jpeg");
        String capText = producer.createText();
        LOG.info("验证码:{}",capText);
        request.getSession().setAttribute("captcha",capText);
        BufferedImage image = producer.createImage(capText);
        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(image,".jpg",out);
        out.flush();
    }

}
